<?php
/**
 * @author Mingming Tang iamalazyrat@gmail.com
 */

class Mall_Controller extends Base_Controller {

  public $restful = true;

  public function get_index() {
    return Redirect::to(url('mall/list'));
  }

  public function get_list() {
    return View::make('mall.list.main');
  }

  public function get_type() {
    return View::make('mall.type.main');
  }

  public function post_type() {
    $input = Input::all();
    $rules = array(
      'cname' => 'required|max:50|unique:types,cname',
      'ename' => 'max:50',
    ); 
    $messages = array(
      'cname_required' => '您没有填写中文类型名称',
      'cname_unique' => '您填写中文类型名称已经有了',
      'cname_max' => '您填写的中文类型名称太长了',
      'ename_max' => '您填写的英文类型名称太长了',
    );
    $validation = Validator::make($input, $rules, $messages);
    if ($validation->fails()) {
      $ret = array('code'=>400, 'msg' => $validation->errors->all());
    } else {
      Type::create($input);
      $ret = array('code'=>200, 'msg' => '操作成功');
    }
    return Response::json($ret);
  }

  public function post_type_edit() {
    $input = Input::all();
    $rules = array(
      'cname' => 'required|max:50|unique:types,cname,' . $input['id'],
      'ename' => 'max:50',
    ); 
    $messages = array(
      'cname_required' => '您没有填写中文类型名称',
      'cname_unique' => '您填写中文类型名称已经有了',
      'cname_max' => '您填写的中文类型名称太长了',
      'ename_max' => '您填写的英文类型名称太长了',
    );
    $validation = Validator::make($input, $rules, $messages);
    if ($validation->fails()) {
      $ret = array('code'=>400, 'msg' => $validation->errors->first());
    } else {
      $type = Type::find($input['id']);
      if ($type) {
        $type->fill($input);
      }
      $type->save();
      $ret = array('code'=>200, 'msg' => '操作成功');
    }
    return Response::json($ret);
  }

  public function post_delete_type() {
    $input = Input::all();
    if (!isset($input['id'])) return Response::json(array('code'=>400, 'msg'=> '参数错误'));
    $type = Type::find($input['id']);
    if (!$type) return Response::json(array('code'=>400, 'msg'=> '找不到'));
    $type->delete();
    return Response::json(array('code'=>200, 'msg'=> '操作成功'));
  }

  public function post_delete_mall() {
    $input = Input::all();
    if (!isset($input['id'])) return Response::json(array('code'=>400, 'msg'=> '参数错误'));
    $mall = Mall::find($input['id']);
    if (!$mall) return Response::json(array('code'=>400, 'msg'=> '找不到'));
    $mall->delete();
    return Response::json(array('code'=>200, 'msg'=> '操作成功'));
  }

  public function post_upload_logo() {
    $logo = Input::file('logo');
    $upload = Config::get('app.upload', '/tmp/'); 
    $logo_dir = $upload . 'logos/';
    if (!is_dir($logo_dir) AND !mkdir($logo_dir) ) {
      return Response::json(array('success'=>false));
    } else {
      $time = time();
      $path = $logo_dir . $time . '_' . $logo['name'];
      $web_path = '/upload/logos/' . $time . '_' .$logo['name'];
      $success = Resizer::open( $logo )
        ->resize( 200 , 400 , 'auto' )
        ->save( $path , 90 );
      //rename($logo['tmp_name'], $path);
      return Response::json(array('success'=>true, 'path'=>$web_path));
    }
  }

  public function post_new() {
    $input = Input::all();
    $rules = array(
      'cname' => 'required|max:50|unique:types,cname',
      'ename' => 'max:50',
      'url' => 'required',
      'description' => 'required',
      'type_id' => 'required',
      'logo_url' => 'required',
    ); 
    $messages = array(
      'cname_required' => '您没有填写中文类型名称',
      'url_required' => '您没有填写网站地址',
      'description_required' => '您没有填写网站描述',
      'type_id_required' => '您没有填写网站类型',
      'logo_url_required' => '您没有上传网站LOGO',
      'cname_required' => '您没有填写中文类型名称',
      'cname_unique' => '您填写中文类型名称已经有了',
      'cname_max' => '您填写的中文类型名称太长了',
      'ename_max' => '您填写的英文类型名称太长了',
    );
    $validation = Validator::make($input, $rules, $messages);
    if ($validation->fails()) {
      $ret = array('code'=>400, 'msg' => $validation->errors->first());
    } else {
      Mall::create($input);
      $ret = array('code'=>200, 'msg' => '操作成功');
    }
    return Response::json($ret);
  }


  public function post_edit_mall() {
    $input = Input::all();
    $rules = array(
      'cname' => 'required|max:50|unique:types,cname,'.$input['id'],
      'ename' => 'max:50',
      'url' => 'required',
      'description' => 'required',
      'type_id' => 'required',
      'logo_url' => 'required',
    ); 
    $messages = array(
      'cname_required' => '您没有填写中文类型名称',
      'url_required' => '您没有填写网站地址',
      'description_required' => '您没有填写网站描述',
      'type_id_required' => '您没有填写网站类型',
      'logo_url_required' => '您没有上传网站LOGO',
      'cname_required' => '您没有填写中文类型名称',
      'cname_unique' => '您填写中文类型名称已经有了',
      'cname_max' => '您填写的中文类型名称太长了',
      'ename_max' => '您填写的英文类型名称太长了',
    );
    $validation = Validator::make($input, $rules, $messages);
    if ($validation->fails()) {
      $ret = array('code'=>400, 'msg' => $validation->errors->first());
    } else {
      $mall = Mall::find($input['id']);
      if ($mall) {
        $mall->fill($input);
      }
      $mall->save();
      $ret = array('code'=>200, 'msg' => '操作成功');
    }
    return Response::json($ret);
  }

}
